package com.cn.wanxi.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class GetSqlSession {
    private static SqlSession sqlSession = null;
    public static SqlSession getSqlSession(){
        String resource = "druid-config.xml";
        SqlSessionFactory sessionFactory = null;
        try {
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            if (sqlSession==null)
                sqlSession = sessionFactory.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }
    public static void commit(){
        if (sqlSession!=null){
            sqlSession.commit();
            sqlSession.close();
            sqlSession = null;
        }
    }
}
